const path = require('path');
const SimpleProgressWebpackPlugin = require('simple-progress-webpack-plugin');
const TerserPlugin = require('terser-webpack-plugin');
const TerserOptions = require('./terser');

function resolve(dir) {
    return path.join(__dirname, dir);
}

const entry = {
    meallogin: {
        // 入口文件
        entry: `src/modules/login/main.js`,
        // 模板文件
        template: 'src/modules/login/index.html',
        // 输出文件
        filename: `meallogin.html`,
        // 页面title
        title: '联登',
        chunks: ['vendors', 'runtime', 'chunk-vendors', 'meallogin']
    },
    mealpay: {
        // 入口文件
        entry: `src/modules/pay/main.js`,
        // 模板文件
        template: 'src/modules/pay/index.html',
        // 输出文件
        filename: `mealpay.html`,
        // 页面title
        title: '支付',
        chunks: ['vendors', 'runtime', 'chunk-vendors', 'mealpay']
    }
};


const _fixNum = num => {
  return num < 10 ? '0' + num : num;
};
const _makeFileName = () => {
  const _d = new Date();
  return `${_d.getFullYear()}${_fixNum((_d.getMonth() + 1))}${_fixNum(_d.getDate())}${_fixNum(_d.getHours())}${_fixNum(_d.getMinutes())}${_fixNum(_d.getSeconds())}`;
};


module.exports = {
  publicPath: './',
  devServer: {
    port: 8088, // 端口
    open: true, // 自动开启浏览器
    compress: false, // 开启压缩
    overlay: {
      warnings: true,
      errors: true
    },
    // // 启动服务后重定向login页
    before(app) {
      let html = `<html><head><title></title>`
      html += `</head><body onload="location.replace('meallogin.html');">`
      html += `</body></html>`
      app.get('/', (req, res) => {
        res.send(html)
      })
    }
  },
  pages: entry, // 入口文件
  outputDir: `./dist/${_makeFileName()}`, // 输出文件目录
  lintOnSave: true, // eslint-loader 是否在保存的时候检查
  // webpack配置
  chainWebpack: (config) => {
    config.resolve.alias
      .set('@', resolve('src'))
      .set('assets', resolve('src/assets'))
      .set('public', resolve('public'))
      .set('static', resolve('public/static'))
      .set('modules', resolve('src/modules'))
  },
  configureWebpack: (config) => {
    if (process.env.NODE_ENV === 'production') {
      // 为生产环境修改配置...
      config.mode = 'production';
      let optimization = {
        runtimeChunk: 'single',
        // 优化打包chunk-vendors.js文件体积过大, 将每个依赖包打包成单独的js文件
        splitChunks: {
            chunks: 'all',
            minSize: 0,
            cacheGroups: {
                vendors: {
                    name: 'vendors',
                    test: /[\\/]node_modules[\\/]/,  // 匹配node_modules目录下的文件
                    priority: -10   // 优先级配置项
                },
                default: {
                    minChunks: 2,
                    priority: -20,   // 优先级配置项
                    reuseExistingChunk: true
                }
            }
        },    
        // 打包时去除打印信息
        minimizer: [new TerserPlugin(TerserOptions)]
      };
      // 代码压缩插件
      Object.assign(config, {
        optimization
      });
      // 打包进度条显示，可以查看到打包进度百分比
      config.plugins.push(
        new SimpleProgressWebpackPlugin()
      );
    } else {
      // 为开发环境修改配置...
      config.mode = 'development';
      // 开发环境开启source-map方便排错
      config.devtool = 'source-map'
    }
  }
}